import java.util.HashMap;
import java.util.Map;

/**
 * @author LKQ
 * @date 2022/1/7 19:08
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String pattern = "aaaa", s = "dog cat cat dog";
        System.out.println(solution.wordPattern(pattern, s));
    }
    public boolean wordPattern(String pattern, String s) {
        String[] arr = s.split(" ");
        if (pattern.length() != arr.length) {
            return false;
        }
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            if (!map.containsKey(pattern.substring(i, i+1)) && !map.containsValue(arr[i])) {
                map.put(pattern.substring(i, i+1), arr[i]);
            }else {
                if (!map.getOrDefault(pattern.substring(i, i + 1), "").equals(arr[i])) {
                    return false;
                }
            }
        }
        return true;
    }
}
